/*
 * @Author: liaoxin
 * @Date: 2022-03-08 17:34:36
 * @LastEditors: Setsuna
 * @LastEditTime: 2023-08-02 11:35:41
 * @FilePath: \src\pages\CustomerManage\Customer\Detail\common\index.tsx
 */
import React from 'react'
import { columnRenders, IUdColumn, routeUtils } from "@/lib"
import apis from '@/apis/CustomerManage'

export enum ETabTypes {
  'BRAND_COMPANY',
  'PRODUCT_AREA',
  'SPECIAL_DELEAR',
  'TERMINAL',
  'SCANNING_CODE'
}

export function getTableColumns(type, history): IUdColumn[] {
  /* 所属品牌公司 */
  const belongBrandCompanyColumns: IUdColumn[] = [
    ['所属品牌公司', 'brandName'],
    ['所属管理区域', 'belongAreaName'],
    ['关联业务员', 'salesmans', (text) => Array.isArray(text) ? text.map((a: any) => <div>{a.salesmanName}</div>) : ''],
    ['业务员联系人电话', 'salesmans', (text) => Array.isArray(text) ? text.map((a: any) => <div>{a.salesmanPhone}</div>) : ''],
    ['是否支持一户多开', 'partnerAgentName', (text,record) => record.partnerAgentName ? '是' : '否'],
    ['关联主考核客户', 'partnerAgentName'],
    ['状态', 'isValid', (text) => columnRenders.enum({ 0: '失效', 1: '生效' })(text)],
    ['盘点权限', 'openCheckStock', (text) => text == 'Y' ? '是' : '否'],
    ['配额申请权限', 'openQuota', (text) => text == 'Y' ? '是' : '否'],
    ['期初库存期间开单数编辑权限', 'allowEditStock', (text) => text == 'Y' ? '是' : '否'],
  ]
  /* 销售产品及区域列 */
  const saleProductsAreasColumns: IUdColumn[] = [
    ["品牌公司","brandName"],
    ['销售物料描述', 'sapMatDescribe'],
    ['物料编码', 'sapMatCode'],
  ]
  /* 下级客户(分销商)列 */
  const underDealerColumns: IUdColumn[] = [
    ['序号', 'id'],
    ['客户名称', 'companyName'],
    ['客户类型', 'agentType'],
    ['联系人姓名', 'contact'],
    ['联系人电话', 'phone'],
    ['地址', 'address'],
    {
      title: '操作',
      dataIndex: 'operate',
      render: columnRenders.operate([
        { title: '查看详情', show: (text, row) => row.agentType, action: (text, row) => toDetail(row) }
      ])
    }
  ]
  /* 下级客户(分销商)列 */
  const underTerminalColumns: IUdColumn[] = [
    ['序号', 'id'],
    ['客户名称', 'terminalName'],
    ['客户类型', 'agentType', () => <div>终端</div>],
    ['联系人姓名', 'contact'],
    ['联系人电话', 'phone'],
    ['地址', 'address'],
  ]
  /**扫码主考核客户 */
  const ScanningCodeCompanyColumns: IUdColumn[] = [
    ['所属品牌公司', 'brandName'],
    ['所属管理区域', 'belongAreaName'],
    ['关联业务员', 'salesmans', (text) => Array.isArray(text) ? text.map((a: any) => <div>{a.salesmanName}</div>) : ''],
    ['业务员联系人电话', 'salesmans', (text) => Array.isArray(text) ? text.map((a: any) => <div>{a.salesmanPhone}</div>) : ''],
    ['是否支持一户多开', 'partnerAgentName', (text,record) => record.partnerAgentName ? '是' : '否'],
    ['关联主考核客户', 'partnerAgentName'],
    ['状态', 'isValid', (text) => columnRenders.enum({ 0: '失效', 1: '生效' })(text)],
    ['盘点权限', 'openCheckStock', (text) => text == 'Y' ? '是' : '否'],
    ['配额申请权限', 'openQuota', (text) => text == 'Y' ? '是' : '否'],
    ['期初库存期间开单数编辑权限', 'allowEditStock', (text) => text == 'Y' ? '是' : '否']
  ]
  function toDetail(row) {
    const { id, agentType = '0', agentCode, belongAreaCode, brandCode, agentName } = row
    history.push('/customer/detail/' + id + '/' + agentType + '?agentCode=' + agentCode + '&belongAreaCode=' + belongAreaCode + '&brandCode=' + brandCode + '&agentName=' + agentName)
  }
  switch (type) {
    case ETabTypes.BRAND_COMPANY: // 所属品牌
      return belongBrandCompanyColumns
    case ETabTypes.PRODUCT_AREA: // 经销产品及区域
      return saleProductsAreasColumns
    case ETabTypes.SPECIAL_DELEAR: // 下级分销商
      return underDealerColumns
    case ETabTypes.TERMINAL: // 下级终端
      return underTerminalColumns
    case ETabTypes.SCANNING_CODE: // 扫码主考核客户
      return ScanningCodeCompanyColumns
  }
}

export function getTabRequestData(type, agentId, pathSearch, queryParams = {}) {
  const searchString = pathSearch
  const defaultPage = { page: 1, size: 10 }
  let params, url, search
  switch (type) {
    case ETabTypes.BRAND_COMPANY:
      search = routeUtils.searchStringToObject(searchString, { agentCode: '', type: 0 })
      params = { ...search, ...queryParams }
      url = apis.BelongBrandCompany
      return { url, params }
    case ETabTypes.PRODUCT_AREA:
      search = routeUtils.searchStringToObject(searchString, { agentCode: '',type: 1})
      params = { ...defaultPage, ...search, ...queryParams }
      url = apis.getOperatingMainPage
      //url = apis.CustomerProductsRegions
      return { url, params }
    case ETabTypes.SPECIAL_DELEAR:
      search = routeUtils.searchStringToObject(searchString, { agentCode: '', belongAreaCode: '', brandCode: '' })
      params = { ...defaultPage, ...search, ...queryParams }
      url = apis.CustomerUnder
      return { url, params }
    case ETabTypes.TERMINAL:
      search = routeUtils.searchStringToObject(searchString, { agentCode: '', brandCode: '' })
      url = apis.TerminalsUnder
      params = { ...defaultPage, ...search, ...queryParams }
      return { url, params }
    case ETabTypes.SCANNING_CODE:
      search = routeUtils.searchStringToObject(searchString, { agentCode: '', type: 1 })
      params = { ...search, ...queryParams }
      url = apis.BelongBrandCompany
      return { url, params }
  }
}